{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. PP-HGNet Introduction\n",
    "PP-HGNet(High Performance GPU Net) is a high-performance backbone network developed by the PaddleCV team that is more suitable for GPU devices.Based on VOVNet, the model uses a learnable downsampling layer(LDS Layer),which integrates the advantages and characteristics of ResNet_vd, PPHGNet and other models,Compared with other SOTA models on GPU devices, this model has higher accuracy under the same latency.At the same latency, the model is 3.8 percentage points higher than the ResNet34-D model, 2.4 percentage points higher than the ResNet50-D model, and 4.7 percentage points higher than the ResNet50-D model after using the SSLD distillation strategy. At the same time, under the same accuracy, its latency is much smaller than the mainstream VisionTransformer model.\n",
    "\n",
    "The PP-HGNet series model was developed and trained based on PaddleClas. For more information on PP-HGNet series models, see [PaddleClas-PPHGNet](https://github.com/PaddlePaddle/PaddleClas/blob/release%2F2.5/docs/en/models/PP-HGNet_en.md)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Performance\n",
    "\n",
    "The evaluation indexes of PP-HGNet series models in ImageNet1k data set are shown as follows:\n",
    "\n",
    "| Model | Top-1 Acc(\\%) | Top-5 Acc(\\%) | Latency(ms) |\n",
    "|:--: |:--: |:--: |:--: | \n",
    "| PPHGNet_tiny      | 79.83 | 95.04 | 1.77 |\n",
    "| PPHGNet_tiny_ssld  | 81.95 | 96.12 | 1.77 |\n",
    "| PPHGNet_small     | 81.51| 95.82 | 2.52  |\n",
    "| PPHGNet_small_ssld | 83.82| 96.81 | 2.52  |\n",
    "| PPHGNet_base_ssld | 85.00| 97.35 | 5.97   |\n",
    "\n",
    "`_ssld` represents the model after using `SSLD distillation`. For details about `SSLD distillation`, see [SSLD distillation](https://github.com/PaddlePaddle/PaddleClas/blob/release/2.5/docs/en/advanced_tutorials/knowledge_distillation_en.md).\n",
    "\n",
    "The comparison between PP-HGNet and other models is as follows. The test machine is NVIDIA® Tesla® V100, the TensorRT engine is turned on, and the precision type is FP32. Under the same latency, the accuracy of PP-HGNet surpasses other SOTA CNN models, and in comparison with the SwinTransformer model, it is more than 2 times faster with higher accuracy.\n",
    "\n",
    "| Model | Top-1 Acc(\\%) | Top-5 Acc(\\%) | Latency(ms) |\n",
    "|:--: |:--: |:--: |:--: |\n",
    "| ResNet34                 | 74.57      | 92.14       | 1.97        |\n",
    "| ResNet34_vd              | 75.98      | 92.98       | 2.00        |\n",
    "| EfficientNetB0           | 77.38      | 93.31       | 1.96        |\n",
    "| <b>PPHGNet_tiny<b>       | <b>79.83<b> | <b>95.04<b> | <b>1.77<b> |\n",
    "| <b>PPHGNet_tiny_ssld<b>  | <b>81.95<b> | <b>96.12<b> | <b>1.77<b> |\n",
    "| ResNet50                 | 76.50      | 93.00       | 2.54        |\n",
    "| ResNet50_vd              | 79.12      | 94.44       | 2.60        |\n",
    "| ResNet50_rsb             | 80.40      |         |     2.54        |\n",
    "| EfficientNetB1           | 79.15      | 94.41       | 2.88        |\n",
    "| SwinTransformer_tiny     | 81.2      | 95.5       | 6.59        |\n",
    "| <b>PPHGNet_small<b>      | <b>81.51<b>| <b>95.82<b> | <b>2.52<b>  |\n",
    "| <b>PPHGNet_small_ssld<b> | <b>83.82<b>| <b>96.81<b> | <b>2.52<b>  |\n",
    "| Res2Net200_vd_26w_4s_ssld| 85.13      | 97.42       | 11.45       |\n",
    "| ResNeXt101_32x48d_wsl    | 85.37      | 97.69       | 55.07       |\n",
    "| SwinTransformer_base     | 85.2       | 97.5        | 13.53       |  \n",
    "| <b>PPHGNet_base_ssld<b> | <b>85.00<b>| <b>97.35<b> | <b>5.97<b>   |"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Quick Start\n",
    "\n",
    "### 3.1 Inference:\n",
    "* Install the relevant Python packages\n",
    "\n",
    "（Remove the\"!\" when not running on the Jupyter Notebook）\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Run the following command to install if CUDA9、CUDA10 or CUDA11 is available."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install paddlepaddle-gpu"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Run the following command to install if GPU device is unavailable."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install paddlepaddle"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Install paddleclas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "jupyter": {
     "outputs_hidden": false
    },
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "!pip install paddleclas"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* Quick Start\n",
    "\n",
    "Congratulations! Now that you've successfully installed PaddleClas, you can experience the image classification effects."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "!wget https://gitee.com/paddlepaddle/PaddleClas/raw/release/2.5/docs/images/inference_deployment/whl_demo.jpg\n",
    "!paddleclas --model_name=PPHGNet_small  --infer_imgs=\"./whl_demo.jpg\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The above command results are as follows:\n",
    "\n",
    "class_ids: [8, 7, 86, 82, 81], scores: [0.71479, 0.08682, 0.00806, 0.0023, 0.00121], label_names: ['hen', 'cock', 'partridge', 'ruffed grouse, partridge, Bonasa umbellus', 'ptarmigan'], filename: docs/images/inference_deployment/whl_demo.jpg\n",
    "Predict complete!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.2 Training\n",
    "* PP-HGNet series models are implemented based on PaddleClas. For details of model training, please refer to[Training, Evaluation and Inference](https://github.com/paddlepaddle/paddleclas/blob/release%2F2.5/docs/en/models/PP-HGNet_en.md)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. Theory"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The author of PP-HGNet analyzes and summarizes the current GPU-friendly networks for GPU devices, and uses 3x3 standard convolutions as much as possible (the highest computational density). Here, VOVNet is used as the base model, and the improvement points that are mainly beneficial to GPU acceleration will be integrated. In the end, under the same latency of PP-HGNet, the accuracy greatly surpasses other backbones.\n",
    "\n",
    "The overall structure of the PP-HGNet backbone network is as follows:\n",
    "\n",
    "<div align=\"center\">\n",
    "<img src=\"https://user-images.githubusercontent.com/12560511/200782647-93cec23d-67fe-4585-bfcf-899dc65c7845.png\"  width = \"80%\"  />\n",
    "</div>\n",
    "\n",
    "Among them, PP-HGNet is composed of multiple HG-Blocks. The details of HG-Blocks are as follows:\n",
    "\n",
    "<div align=\"center\">\n",
    "<img src=\"https://user-images.githubusercontent.com/12560511/201665345-1182b902-29f2-43a4-bc9b-c3473769b025.png\"  width = \"80%\"  />\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5. Notes\n",
    "The test machine is NVIDIA® Tesla® V100, the TensorRT engine is turned on, and the precision type is FP32. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6. Relevant Papers and Citations"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
